// use/door/door.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ids:"",
    qrcode:"",
    tip: "请开启卷帘门！",
    last_use_time: "",
    imagePath: "/images/door.png",
    percent: 0,
    progress: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.ids)
    var that = this;
    // ids = ' + JSON.stringify(data) + ' & qrcode=' + code + ' & last_use_time=' + that.data.last_use_time,
    var ids = options.ids;
    var qrcode = options.qrcode;
    var last_use_time = options.last_use_time ? options.last_use_time : 0;
    this.setData({
      ids: JSON.parse(ids),
      qrcode: qrcode,
      last_use_time: last_use_time
    });
    if (options.qrcode) {
      var qrcode = options.qrcode;
      app.getRequest("api/device/qrcodeDevice", { device_qrcode_id: qrcode }, function (res) {
        if (res.code === 200) {
          var status = res.data.device_status
          // if (status === 1 && res.data.has_rolling != 0) {
          //   wx.showModal({
          //     title: '操作',
          //     content: '请确定打开卷帘门',
          //     confirmText: '确定',
          //     showCancel: true,
          //     success: (e) => {
          //       console.log(e);
          //       if (e.confirm) {
          //         that.openDoor();
          //       } else {
          //         wx.navigateBack()
          //       }
          //     }
          //   })
          // }
          if (status !== 1) {
            wx.redirectTo({
              url: '/use/work/work?ids=' + JSON.stringify(that.data.ids) + '&qrcode=' + that.data.qrcode + '&last_use_time=' + that.data.last_use_time,
              // url: '/use/door/door?ids=' + JSON.stringify(data) + '&qrcode=' + code + '&last_use_time=' + that.data.last_use_time,
            })
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: "none"
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 开门
  openDoor() {
    var that = this;
    // wx.showLoading({
    //   title: '正在开门....',
    // })
    // console.log(this.data)
    var params = {
      product_iot_id: this.data.ids.product_iot_id,
      device_id: this.data.ids.device_id,
      operate: "open"
    }
    var num = 0;
    that.setData({
      percent: 0,
      progress: true
    })
    this.setI = setInterval(function () {
      num = num + 2;
      if (num >= 95) {
        clearInterval(that.setI);
      } else {
        that.setData({
          percent: num
        })
      }
    }, 200);
    app.getRequest("api/device/opendoor", params, function (res) {
      if (res.code == 200) {
        wx.showLoading({
          title: '正在开门....',
        })
        app.getRequestSync("api/device/mnstest", { msgid: res.data.msgId }, function (req) {
          wx.hideLoading();
          if (res.code == 200 && req.data.data.success) {
            console.log(res)
            that.setData({
              progress: false,
              percent: 100
            })
            
            if (req.data.data.success != "false"){
              wx.showToast({
                title: '开启成功！',
              });
              console.log(that.data.ids)
              wx.redirectTo({
                url: '/use/work/work?ids=' + JSON.stringify(that.data.ids) + '&qrcode=' + that.data.qrcode + '&last_use_time=' + that.data.last_use_time,
                // url: '/use/door/door?ids=' + JSON.stringify(data) + '&qrcode=' + code + '&last_use_time=' + that.data.last_use_time,
              })
            }else{
              that.setData({
                progress: false,
                percent: 0
              })
              wx.showToast({
                title: '开启失败！',
                icon:'none'
              });
            }
            
          } else {
            // that.openDoor();
            that.setData({
              progress: false,
              percent: 0
            })
            wx.showToast({
              title: req.message,
              icon: "none"
            })
          }
        })
      }
    })
  },
})